package com.yinhai.httpclient;

import java.util.concurrent.TimeUnit;

import org.apache.http.conn.ClientConnectionManager;

public class IdleConnEvictorThread extends Thread {
	
	private final ClientConnectionManager connManager;
	private volatile boolean shutdownFlag = false;
		
	public IdleConnEvictorThread(ClientConnectionManager connManager) {
		super();
		this.connManager = connManager;
	}
	
	public void shutdown() {
		synchronized (this) {
			shutdownFlag = true;
			notifyAll();
		}
	}
	
	public void run() {
		try {
			while(! shutdownFlag) {
				try {
					synchronized(this) {
						try {
							wait(HttpClientContext.HTTPCLIENT_CONN_TIMERMSEL);
						} catch(InterruptedException e) {		}
						
						try {
							connManager.closeExpiredConnections();
							connManager.closeIdleConnections(HttpClientContext.HTTPCLIENT_CONN_IDLESECEND, TimeUnit.SECONDS);
						} catch(Exception e) {		}
					}
				} catch(Throwable e) {	 }
			}
		} catch(Throwable e) {	}
	}	
	
}
